﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using Dukey.Model;
using Dukey.DBUtility;

namespace BLL
{
    /// <summary>
    /// 版块表
    /// </summary>
    public class Channel
    {        
        public static readonly Channel instance = new Channel();
 
     
        public List<Dukey.Model.Channel> GetList(string strWhere)
        {
            List<Dukey.Model.Channel> list = new List<Dukey.Model.Channel>();
            StringBuilder strSql = new StringBuilder();

            Dukey.Model.WebConfig mysite = BLL.WebConfig.instance.GetModelByCache();
            if(mysite!=null)
            {
                if (mysite.showtype == 2)//表示静态页面
                {
                    strSql.Append("select bid,cname,ename,fid,xpath,note_num,linktohtml as link,showid,px,page_title,remark,isshow,ismain,cur_time,linkto,linktohtml ");
                }
                else
                {
                    strSql.Append("select bid,cname,ename,fid,xpath,note_num,linkto as link,showid,px,page_title,remark,isshow,ismain,cur_time,linkto,linktohtml ");
                }
            }
            strSql.Append(" FROM Channel ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by px,bid");
            
            OleDbDataReader dr = DbHelper.ExecuteReader(strSql.ToString());
            while (dr.Read())
            {
                Dukey.Model.Channel model = new Dukey.Model.Channel();
                if (dr["bid"].ToString() != "")
                {
                    model.bid = int.Parse(dr["bid"].ToString());
                }
                model.cname = dr["cname"].ToString();
                model.ename = dr["ename"].ToString();
                if (dr["fid"].ToString() != "")
                {
                    model.fid = int.Parse(dr["fid"].ToString());
                }
                if (dr["note_num"].ToString() != "")
                {
                    model.note_num = int.Parse(dr["note_num"].ToString());
                }
                model.xpath = dr["xpath"].ToString();
                model.link = dr["link"].ToString();
                if (dr["showid"].ToString() != "")
                {
                    model.showid = int.Parse(dr["showid"].ToString());
                }
                if (dr["px"].ToString() != "")
                {
                    model.px = int.Parse(dr["px"].ToString());
                }
                model.page_title = dr["page_title"].ToString();
                model.remark = dr["remark"].ToString();
                if (dr["isshow"].ToString() != "")
                {
                    if ((dr["isshow"].ToString() == "1") || (dr["isshow"].ToString().ToLower() == "true"))
                    {
                        model.isshow = true;
                    }
                    else
                    {
                        model.isshow = false;
                    }
                }
                if (dr["ismain"].ToString() != "")
                {
                    if ((dr["ismain"].ToString() == "1") || (dr["ismain"].ToString().ToLower() == "true"))
                    {
                        model.ismain = true;
                    }
                    else
                    {
                        model.ismain = false;
                    }
                }
                if (dr["cur_time"].ToString() != "")
                {
                    model.cur_time = DateTime.Parse(dr["cur_time"].ToString());
                }
                list.Add(model);
                model.linkto = dr["linkto"].ToString();
                model.linktohtml = dr["linktohtml"].ToString();

            }
            dr.Dispose();
            return list;
        }
        
        public List<Dukey.Model.Channel> GetListByCache(string strWhere)
        {
            object objModel = Common.DataCache.GetCache("BoardList"+StringHelper.ReplaceBadChar(strWhere));
            if (objModel == null)
            {
                try
                {
                    objModel = GetList(strWhere);
                    if (objModel != null)
                    {
                        Common.DataCache.SetCache("BoardList"+StringHelper.ReplaceBadChar(strWhere), objModel);
                    }
                }
                catch { }
            }
            return objModel as List<Dukey.Model.Channel>;         
        }

    }


}
